package checkers3d.logic;

import checkers3d.presentation.WindowFileBrowser;
import checkers3d.presentation.IInputObserver;
import checkers3d.storage.DataManagerGame;
import java.awt.Point;

/**
 * Shows a list of gamess and allows the player to play one.
 *
 * @author Ruben Acuna
 */
public class MenuLoad extends WindowFileBrowser
{
    /**
     * File extension type for this window file browser.
     */

    // Static so that the constructor can see it...
    static private final String FILE_EXTENSION = ".txt";

    /**
     * Class constructor that requires the window size. It creates control
     * buttons, game list.
     *
     * @param size Size of window.
     */
    public MenuLoad(Point size)
    {
        super("Save", DataManagerGame.LOCATION_SAVEGAMES, FILE_EXTENSION, size);

        setStartObserver(new ObserverLogin(this));
    }

    /**
     * Returns a string representation of this object containing it's class name.
     *
     * @return String representation.
     */
    @Override
    public String toString()
    {
        return getClass().getName();
    }

    /**
     * A observer for a button displays the player login window.
     */
    private class ObserverLogin implements IInputObserver
    {
        /**
         * Reference to the parent MenuLoad object, use for finding
         * the current scenario name.
         */
        private MenuLoad parent;

        /**
         * Class constructor that keeps a reference to the parent object.
         *
         * @param parent Reference to the parent MenuLoad object.
         */
        public ObserverLogin(MenuLoad parent)
        {
            this.parent = parent;
        }

        /**
         * Displays the player login window.
         *
         * @param position Location of click within button.
         */
        public void onClick(Point position)
        {
            String[] playerNames = parent.getSelectedFilename().split("&");
            String usernameHome = playerNames[0];
            String usernameVisitor = playerNames[1];

            GameMaster.getInstance().addGUIContainer(new MenuLoadGameLoginMP(getSize(),
                    usernameHome, usernameVisitor));
        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }
}